package com.example.yls.demoa;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by yls on 2018/12/3.
 */

public class StuContentProvider extends ContentProvider {

    private StuOpenHelper stuOpenHelper;
    private static final String AUTOHORITY = "com.gdcp.student";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTOHORITY,"student",1);
        uriMatcher.addURI(AUTOHORITY,"grade",2);
    }

    @Override
    public boolean onCreate() {
        stuOpenHelper = DBDao.getOpenHelper(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        Cursor cursor = null;
        SQLiteDatabase db = DBDao.getDB(getContext());
        switch (uriMatcher.match(uri)){
            case 1:
                cursor = db.query("student",strings,s,strings1,s1,null,null);
                break;
            case 2:
                break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        SQLiteDatabase db = DBDao.getDB(getContext());
        switch (uriMatcher.match(uri)){
            case 1:
                db.insert("student",null,contentValues);
                break;
            case 2:
                break;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        SQLiteDatabase db = DBDao.getDB(getContext());
        switch (uriMatcher.match(uri)){
            case 1:
                return db.delete("student",null,null);
            case 2:
                break;
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        SQLiteDatabase db = DBDao.getDB(getContext());
        switch (uriMatcher.match(uri)){
            case 1:
                return db.update("student",contentValues,s,strings);
            case 2:
                break;
        }
        return 0;
    }
}
